app.controller('bpmTraceViewController', ['$scope', 'httpWrapService', '$window', '$state', '$stateParams','GridFactory','$sce','$uibPosition',
        function($scope, httpWrapService, $window, $state, $stateParams,GridFactory,$sce,$uibPosition) {

    //$scope.generateDiagram = false;

    $scope.e_offset = {
        left : 0,
        top : 0
    }


    httpWrapService.defaultPost('bpm/process/trace/view/' + $stateParams.executionId + '.do' ).then(function(res){
        $scope.historicProcessInstance = res.data.entity;
        $scope.processDefinition = res.data.map.processDefinition;
        $scope.activities = res.data.map.activities;
        $scope.formProperties = res.data.map.formProperties;
        $scope.variableInstances = res.data.map.variableInstances;
        $scope.imgUrl = 'bpm/resource/read.do?pdid=' + $scope.historicProcessInstance.processDefinitionId + '&resourceName=' + $scope.processDefinition.diagramResourceName;
    });

    $scope.$watch("generateDiagram", function(newValue,oldValue, scope) {
        if(!$scope.generateDiagram && !$scope.infos) {
            httpWrapService.defaultPost('bpm/process/trace/data/' + $stateParams.executionId + '.do' ).then(function(res){
                $scope.infos = res.data.data;
            });
        }
        if(!$scope.generateDiagram && $scope.historicProcessInstance){
            $scope.imgUrl = 'bpm/process/trace/data/auto/' + $scope.historicProcessInstance.processInstanceId +'.do';
        }else if($scope.historicProcessInstance) {
            $scope.imgUrl = 'bpm/resource/read.do?pdid=' + $scope.historicProcessInstance.processDefinitionId + '&resourceName=' + $scope.processDefinition.diagramResourceName;
        }
    })


    // 延迟读取图片位置
    setTimeout(function () {
        var divEl = $window.document.querySelector('#generateDiagram');
        $scope.e_offset = $uibPosition.offset(divEl);
        $scope.$apply();
    }, 500)

}]);
